 $(function () {
        $(".checkbox").click(function () {
            console.log("chang state");
            var $checkbox = $(this);
            var $tr = $checkbox.parents("tr");
            var cartsid = $tr.attr("cartsid");

            $.getJSON('/Changecarts/', {'cartsid': cartsid}, function (data) {
                console.log(data);
                if (data['status'] === 200) {
                    $("#total_price").html(data['total_price']);
                    if (data['c_is_select']) {
                        $checkbox.find("span").html("✔");
                    } else {
                        $checkbox.find("span").html("-");
                    }
                    if (data['is_all_select']) {
                        $(".all_select span span").html("✔");
                    } else {
                        $(".all_select span span").html("");
                    }

                }
            })
        });
        $(".sub").click(function () {
            var $sub = $(this);
            var $tr = $sub.parents("tr");
            var cartsid = $tr.attr("cartsid");
            window.alert("减少成功,请刷新页面");
            location.replace(location.href);
            $.getJSON("/subshoppings/", {'cartsid': cartsid}, function (data) {
                console.log(data);
                if (data[status] === 200) {
                    $("#total_price").html(data['total_price']);
                    if (data['c_goods_num'] > 0) {
                        var $em = $sub.next("em");
                        $em.html(data['c_goods_num']);

                    } else {
                        $tr.remove()
                    }
                }
            })


        });
        $(".add").click(function () {
            var $add = $(this);
            var $tr = $add.parents("tr");
            var cartsid = $tr.attr("cartsid");
            window.alert("添加成功,请刷新页面");
            location.replace(location.href);
            $.getJSON("/addshoppings/", {'cartsid': cartsid}, function (data) {
                console.log(data);
                if (data[status] === 200) {
                    $("#total_price").html(data['total_price']);
                    if (data['c_goods_num'] > 0) {
                        var $em = $sub.next("em");
                        $em.html(data['c_goods_num']);


                    } else {
                        window.alert("没有库存");
                    }
                }
            })


        });
        $(".all_select").click(function () {
            var $all_select = $(this);
            var select_list = [];
            var unselect_list = [];
            $(".CH").click(function () {
                var $CH = $(this);
                var cartid = $CH.parents("td").attr("cartid");
                if ($CH.find("button").html().trim()) {
                    select_list.push(cartid);
                } else {
                    unselect_list.push(cartid);
                }
            })
            if (unselect_list.length > 0) {
                $.getJSON("/allselect/", {'cart_list': unselect_list.join("#")}, function (data) {
                    console.log(data);
                    if (data['status'] === 200) {
                        $(".CH").find("button").html("✔");
                        $all_select.find("button").html("✔");
                    }
                })

            } else {
                if (select_list.length > 0) {
                    $.getJSON("/allselect/", {'cart_list': select_list.join("#")}, function (data) {
                        console.log(data);
                        if (data['status'] === 200) {
                            $(".CH").find("button").html("");
                            $all_select.find("button").html("");
                        }
                    })
                }


            }
        });
        $(".HGW").click(function () {
            window.alert("购买成功,请刷新页面");
            location.replace(location.href);
            $.getJSON("/pay/", function (data) {
                console.log(data);
                var target;
                if (data['status'] === 200) {
                    window.open("/order/?ordrid=" + data['order_id'], target = "_self");
                }
            })
        })
        $("#click").click(function () {
            window.alert("提交成功");

        })

    })